package com.workcenter.controller.Sys;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.Framework.Base.BaseController;
import com.common.annotation.Log;
import com.common.entity.Tree;
import com.common.utils.MD5Utils;
import com.common.utils.R;
import com.common.utils.ShiroUtils;
import com.workcenter.entity.Sys.MenuDO;
import com.workcenter.entity.Sys.SysUserDO;
import com.workcenter.service.MenuService;

@Controller
public class LoginController extends BaseController {
  private final Logger logger = LoggerFactory.getLogger(this.getClass());

  @Autowired
  MenuService menuService;

  @Log("请求访问主页")
  @RequestMapping({"/", "", "/index"})
  String Index(Model model) {
    // Tree<MenuDO> menuTree = menuService.getSysMenuTree(getUserId());
    SysUserDO user = getUser();
    List<Tree<MenuDO>> menus = menuService.listMenuTree(user.getUserId());
    System.out.println(menus);
    model.addAttribute("menus", menus);
    model.addAttribute("name", user.getName());
    logger.info(user.getName());
    return "index";
  }

  @RequestMapping("/icon")
  String icon() {
    return "FontIcoList";
  }

  @RequestMapping("/login")
  String login() {
    return "login";
  }

  @RequestMapping("/login_bak")
  String doLogin(String username, String password) {
    password = MD5Utils.encrypt(username, password);
    UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    //    token.setRememberMe(true);
    Subject subject = SecurityUtils.getSubject();
    try {
      subject.login(token);
      return "redirect:/index";
    } catch (AuthenticationException e) {
      System.out.println("登录失败信息------" + e.getMessage());
      return "redirect:/login";
    }
  }

  @Log("登录")
  @RequestMapping("/Syslogin/login")
  @ResponseBody
  R ajaxLogin(String username, String password) {
    password = MD5Utils.encrypt(username, password);
    UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    //    token.setRememberMe(true);
    Subject subject = SecurityUtils.getSubject();
    try {
      //      if((System.currentTimeMillis()-subject.getSession().getStartTimestamp().getTime())>=lengthenTimeOut-1000){  
      //        ThreadContext.remove(ThreadContext.SUBJECT_KEY);//移除线程里面的subject  
      //        shiroSessionManager.getSessionDAO().delete(subject.getSession());//移除失效的session 
      subject.login(token);
      //      }
      return R.ok();
    } catch (AuthenticationException e) {
      return R.error("用户或密码错误");
    }
  }

  @RequestMapping("/logout")
  String logout() {
    ShiroUtils.logout();
    return "redirect:/login";
  }

  @RequestMapping("/main")
  String main() {
    return "main";
  }

  @RequestMapping("/403")
  String error403() {
    return "403";
  }

}
